package com.kd.juhaowan.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kd.juhaowan.R;
import com.kd.juhaowan.http.api.CommonApi;
import com.kd.juhaowan.http.model.HttpListData;
import com.kd.juhaowan.http.model.SigninModel;
import com.kd.juhaowan.ui.adapter.BaseRecyclerViewAdapter;
import com.kd.juhaowan.ui.dialog.ConfirmDialog;
import com.kd.juhaowan.ui.dialog.SigninConfirmDialog;
import com.kd.juhaowan.ui.holder.SigninViewHolder;
import com.kd.juhaowan.ui.listener.OnItemClickListener;
import com.kd.juhaowan.utils.FullyGridLayoutManager;
import com.kd.juhaowan.view.RecycleViewForScrollView;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SigninActivity extends BaseActivity  implements OnItemClickListener {


    @BindView(R.id.activity_title)
    TextView titleView;
    @BindView(R.id.signin_month)
    TextView monthView;
    @BindView(R.id.signin_day)
    TextView totalView;





    private List<SigninModel> models;
    @BindView(R.id.signin_listview)
    RecycleViewForScrollView m648ListView;
    private BaseRecyclerViewAdapter<SigninViewHolder, SigninModel> m648Adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleView.setText("每日签到");
        setAdapter();
//        EasyHttp.post(this).api(new CommonApi("api/signin/list")).request(new HttpCallbackProxy<HttpData<MoreDjqResult>>(this){
//            @SuppressLint("SuspiciousIndentation")
//            @Override
//            public void onHttpSuccess(HttpData<MoreDjqResult> result, boolean cache) {
//                super.onHttpSuccess(result, cache);
//
//            }
//        });
       initdata();
    }

    @Override
    protected void reInitDate() {
        initdata();
    }

    private void initdata(){
        EasyHttp.post(this).api(new CommonApi("api/signin/list")).request(new HttpCallbackProxy<HttpListData<SigninModel>>(this){
            @Override
            public void onHttpSuccess(HttpListData<SigninModel> result, boolean cache) {
                super.onHttpSuccess(result, cache);
                monthView.setText(result.getData().getMonth()+"");
                totalView.setText(result.getData().getAccum_days()+"");
                models =result.getData().getList();
                m648Adapter.resetAdapterData(models);
            }
        });
    }

    private void setAdapter(){
        m648Adapter = new BaseRecyclerViewAdapter<SigninViewHolder, SigninModel>() {
            @NonNull
            @Override
            public SigninViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                View view = inflater.inflate(R.layout.item_signin, null, false);
                SigninViewHolder holder = new SigninViewHolder(view);
                holder.setOnItemClickListener(getOnItemClickListener());
                return holder;
            }

            @Override
            public void onBindViewHolder(@NonNull SigninViewHolder holder, int position) {
                holder.setDate(getItem(position));
            }
        };

        m648Adapter.setOnItemClickListener(SigninActivity.this);
        m648Adapter.resetAdapterData(null);
//        LinearLayoutManager layoutManager = new LinearLayoutManager(SigninActivity.this);
//        layoutManager.setOrientation(RecyclerView.HORIZONTAL);
        FullyGridLayoutManager manager = new FullyGridLayoutManager(this,
                4, GridLayoutManager.VERTICAL, false){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };

        m648ListView.setLayoutManager(manager);
        m648ListView.setAdapter(m648Adapter);



    }


    @Override
    public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int position) {

    }
    private ConfirmDialog dialog = null;
//    @Override
//    public void onItemDianzanClick(BaseViewHolder viewHolder, int position) {
//        GiftModel model = m648Adapter.getItem(position);
//        EasyHttp.post(this).api(new ReciveDjqApi(model.getId(),DeviceUtil.isInstallApp(this,model.getGame_package_name())?1:2)).request(new HttpCallbackProxy<HttpData<GiftModel>>(this){
//            @Override
//            public void onHttpSuccess(HttpData<GiftModel> result, boolean cache) {
//                super.onHttpSuccess(result, cache);
//                model.setIs_receive(1);
////                    model.setCode(result.getMessage());
//                m648Adapter.notifyItemChanged(position,model);
//                startDownload(model);
//                ConfirmDialog.Builder builder = new ConfirmDialog.Builder(SigninActivity.this);
//                builder.setMessage(result.getMessage());
//                builder.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        gotGameInfo(model);
//                        dialog.dismiss();
//                    }
//                });
//                dialog = builder.create();
//                dialog.show();
//            }
//        });
//    }




    @OnClick(R.id.actvity_back)
    public void onBackClick(){
        finish();
    }

    @OnClick(R.id.signin_rule)
    public void onRuleClick(){
        Intent in=new Intent(SigninActivity.this,DocumentActivity.class);
        in.putExtra("question_id","20");
        startActivity(in);
    }

    @OnClick(R.id.signin_confirm)
    public void onConfirmClick(){

        EasyHttp.post(this).api(new CommonApi("api/signin/clickSingin")).request(new HttpCallbackProxy<HttpListData<SigninModel>>(this){
            @Override
            public void onHttpSuccess(HttpListData<SigninModel> result, boolean cache) {
                super.onHttpSuccess(result, cache);
                SigninConfirmDialog.Builder builder = new SigninConfirmDialog.Builder(SigninActivity.this);
                builder.setMessage(result.getData().getList());
                builder.create().show();
                initdata();
            }
        });


    }














//    @OnClick(R.id.actvity_back)
//    public void onBackClick(){
//        finish();
//    }
    @Override
    protected int getViewLayoutResourceId() {
        return R.layout.actviity_signin;
    }




}
